﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YK.Component.Data.ServiceBuilder
{
    using YK.Component.Data;
    using YK.Component.Tools;

    public abstract class ServiceBase<T> : IService<T> where T : EntityBase
    {
        protected IUnitOfWork UnitOfWork { get; private set; }

        protected IRepository<T> CurrentRepository { get; private set; }

        public ServiceBase(IUnitOfWork unitOfWork,IRepository<T> repository)
        {
            UnitOfWork = unitOfWork;
            CurrentRepository = repository;
        }

        #region Interface

        public T AddEntity(T entity)
        {
            CurrentRepository.Add(entity);
            UnitOfWork.Commit();
            return entity;
        }

        public bool UpdateEntity(T entity)
        {
            CurrentRepository.Update(entity);
            UnitOfWork.Commit();
            return true;
        }

        public bool DeleteEntity(T entity)
        {
            CurrentRepository.Delete(entity);
            UnitOfWork.Commit();
            return true;
        }

        /************************select*********************/
        public T GetById(string id)
        {
            return CurrentRepository.GetById(id);
        }

        public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            return CurrentRepository.GetAll().Where(whereLambda);
        }

        public IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, bool isAsc, System.Linq.Expressions.Expression<Func<T, S>> orderByLambda)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
